use std::sync::{atomic::Ordering, Arc};

use axum::{response::IntoResponse, routing::post, Extension, Json, Router};
use serde::Deserialize;

use crate::{
    server::{ApiOk, AppState, RecordCmd},
    Result,
};

pub fn router() -> Router {
    Router::new().route("/", post(set_record))
}

#[derive(Debug, Deserialize)]
struct PostData {
    cmd: RecordCmd,
}
async fn set_record(
    Extension(app_state): Extension<Arc<AppState>>,
    body: String,
) -> Result<impl IntoResponse> {
    let post_data = serde_json::from_str::<PostData>(&body)?;

    let is_record = bool::from(post_data.cmd);

    app_state.is_record.store(is_record, Ordering::Relaxed);

    Ok(Json(ApiOk::OK))
}
